//
//  SQLiteTaxDao.m
//  PrivateTaxCalculator
//
//  Created by albert ma on 1/5/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "SQLiteTaxDao.h"
#import <sqlite3.h>

static sqlite3 *database = nil;
NSMutableArray *tax2011 = nil;
NSMutableArray *tax2009 = nil;
static SQLiteTaxDao *myinstance = nil;
@implementation SQLiteTaxDao

+(id) instance
{
    if(myinstance == nil)
    {
        myinstance = [[SQLiteTaxDao alloc] init];
    }
    return myinstance;
}

-(void)initalization:(NSString*)dBPath
{
    if (!tax2011)
    {
        tax2011 = [[NSMutableArray alloc]init];
    }
    if (!tax2009) 
    {
        tax2009 = [[NSMutableArray alloc]init];
    }
    
	if (sqlite3_open([dBPath UTF8String], &database) == SQLITE_OK) {
		
		const char *sql = "select level, bottom, top, rate, deduction, tax_base from tax2011";
		sqlite3_stmt *selectstmt;
		if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
			
			while(sqlite3_step(selectstmt) == SQLITE_ROW) {
				

                PrivateTax *privateTax = [[PrivateTax alloc]init];
                [privateTax setLevel:[NSNumber numberWithInt:sqlite3_column_int(selectstmt, 0)]];
                [privateTax setBottom:[NSNumber numberWithDouble:sqlite3_column_double(selectstmt, 1)]];
                [privateTax setTop:[NSNumber numberWithDouble:sqlite3_column_double(selectstmt, 2)]];
                [privateTax setRate:[NSNumber numberWithDouble:sqlite3_column_double(selectstmt, 3)]];
                [privateTax setDeduction:[NSNumber numberWithDouble:sqlite3_column_double(selectstmt, 4)]];
                [privateTax setTaxBase:[NSNumber numberWithDouble:sqlite3_column_double(selectstmt, 5)]];
                [tax2011 addObject:privateTax];
                [privateTax release];
                
            }
		}
        const char *sql1 = "select level, bottom, top, rate, deduction, tax_base from tax2009";
		
		if(sqlite3_prepare_v2(database, sql1, -1, &selectstmt, NULL) == SQLITE_OK) {
			
			while(sqlite3_step(selectstmt) == SQLITE_ROW) {
				
                PrivateTax *privateTax = [[PrivateTax alloc]init];
                [privateTax setLevel:[NSNumber numberWithInt:sqlite3_column_int(selectstmt, 0)]];
                [privateTax setBottom:[NSNumber numberWithDouble:sqlite3_column_double(selectstmt, 1)]];
                [privateTax setTop:[NSNumber numberWithDouble:sqlite3_column_double(selectstmt, 2)]];
                [privateTax setRate:[NSNumber numberWithDouble:sqlite3_column_double(selectstmt, 3)]];
                [privateTax setDeduction:[NSNumber numberWithDouble:sqlite3_column_double(selectstmt, 4)]];
                [privateTax setTaxBase:[NSNumber numberWithDouble:sqlite3_column_double(selectstmt, 5)]];
                [tax2009 addObject:privateTax];
                [privateTax release];
                
            }
		}
        sqlite3_close(database);
	}
	else
		sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
}
-(PrivateTax*) getCurrentPrivateTax:(double)taxIncome versionof:(TaxType)version
{
    PrivateTax* currPrivateTax = nil;
    int count = 0;
    if(taxIncome == 0.0f)
    {
        return nil;
    }
    NSMutableArray* currentArray = NULL;
    if (version == TAX_2011) 
    {
        currentArray = tax2011;
    }
    else
    {
        currentArray = tax2009;
    }
    for (PrivateTax* privateTax in currentArray) {
        double bottom = [[privateTax bottom] doubleValue];
        double top = [[privateTax top]doubleValue];
        count++;
        if (count == [currentArray count]) {
            currPrivateTax = privateTax;
            break;
        }
        else
        {
            if(taxIncome > bottom && taxIncome <= top)
            {
                currPrivateTax = privateTax;
                break;
            }
        }
    }
    return currPrivateTax;
    
}


-(int) getTaxLevel:(double) taxIncome versionof:(TaxType)version
{
    PrivateTax* currPrivateTax = [self getCurrentPrivateTax:taxIncome versionof:version];
    if(currPrivateTax == nil)
    {
        return 0;
    }

    return [[currPrivateTax level] intValue];
}

-(double) getTaxRate:(double) taxIncome versionof:(TaxType)version
{
    PrivateTax* currPrivateTax = [self getCurrentPrivateTax:taxIncome versionof:version];
    if(currPrivateTax == nil)
    {
        return 0;
    }

    return [[currPrivateTax rate]doubleValue];
}

-(double) getDeducation:(double)taxIncome versionof:(TaxType)version
{
    PrivateTax* currPrivateTax = [self getCurrentPrivateTax:taxIncome versionof:version];
    if(currPrivateTax == nil)
    {
        return 0;
    }
    
    return [[currPrivateTax deduction] doubleValue];
}


-(double) getTaxBase:(TaxType)version
{
    if (version == TAX_2011) {
        if ([tax2011 count] > 0) {
            PrivateTax* privateTax = [tax2011 objectAtIndex:0];
            return [[privateTax taxBase] doubleValue];
        }
    }
    else
    {
        if ([tax2009 count] > 0) {
            PrivateTax* privateTax = [tax2009 objectAtIndex:0];
            return [[privateTax taxBase] doubleValue];
        }
    }
    return 0;
}

-(void)dealloc
{
    [tax2011 release];
    [tax2009 release];

    [super dealloc];
}

@end
